package com.wish.datastructure;

/**
 * Created by suliucheng on 2021/4/11 22:02
 * Directions: 用数组来代替栈本身  定义一个栈顶指针 top   定义栈底为 0  栈深度 maxSize
 */
public class Stack {
    private String[] arr;
    private int top;
    private int maxSize;

    public Stack(int maxSize) {
        this.maxSize = maxSize;
        this.arr = new String[maxSize];
        this.top = -1;
    }

    public void list() {
        for (String i : arr) {
            System.out.print(i + " ");
        }
    }

    public String pop() {
        if (stackIsEmpty()) {
            throw new IndexOutOfBoundsException("栈内存已空，不能再移除元素");
        }
        String topStr = arr[top];
        arr[top--] = null;
        return topStr;
    }

    public String push(String num) {
        if (stackIsFull()) {
            throw new IndexOutOfBoundsException("栈内存已满，不能再添加元素");
        }
        arr[++top] = num;
        return num;
    }

    public String cloneTop() {
        return arr[top];
    }

    public boolean stackIsFull() {
        return top == maxSize - 1;
    }

    public boolean stackIsEmpty() {
        return top == -1;
    }

}
